﻿//#define Native64BitApplication

using System;
using System.Collections.Generic;
using System.Web;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using System.Web.SessionState;

/// <summary>
/// Support for functionality specific to ArcGIS Server Local datasources.  
/// 
/// To run this application in a 64-bit application pool:
/// 1) Uncomment the "#define Native64BitApplication" preprocessor directive at the top of this file.  
/// 2) Comment out the two sections in the web.config file beginning with "<!--Begin Remove for 64-bit native deployment-->" 
/// and ending with "<!--End Remove for 64-bit native deployment-->"
/// 3) Search for "esriEditor:EditorTask" in the .skin files and delete the entire XML element.
/// </summary>
public static class ArcGISServerLocalSupport
{
    public static bool HasNonPooledResources(MapResourceManager mapManager, GeocodeResourceManager geocodeManager,
        GeoprocessingResourceManager geoproccessingManager)
    {
#if Native64BitApplication
            return false;
#else
        return (
            ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal.HasNonPooledServices(mapManager) ||
            ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal.HasNonPooledServices(geocodeManager) ||
            ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal.HasNonPooledServices(geoproccessingManager));
#endif
    }

    public static void ReleaseNonPooledContexts()
    {
#if Native64BitApplication
        return;
#else
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal.ReleaseNonPooledContexts(HttpContext.Current.Session);
#endif
    }

    public static void SessionCleanup()
    {
        HttpSessionState session = HttpContext.Current.Session;
        // Code that runs when a session is abandoned. 
  #if Native64BitApplication
         for (int i = 0; i < session.Count; i++)
        {
             if (session[i] is IDisposable)
                ((IDisposable)session[i]).Dispose();
        }
#else
        System.Collections.Generic.List<ESRI.ArcGIS.Server.IServerContext> contexts = new System.Collections.Generic.List<ESRI.ArcGIS.Server.IServerContext>();
        for (int i = 0; i < session.Count; i++)
        {
            if (session[i] is ESRI.ArcGIS.Server.IServerContext)
                contexts.Add((ESRI.ArcGIS.Server.IServerContext)session[i]);
            else if (session[i] is IDisposable)
                ((IDisposable)session[i]).Dispose();
        }

        foreach (ESRI.ArcGIS.Server.IServerContext context in contexts)
        {
            context.RemoveAll();
            context.ReleaseContext();
        }
#endif

    }
}
